import asyncio
import time

import websockets

async def handle_connection(websocket):
    async for message in websocket:
        print(f"收到客户端消息: {message}")
        response = f"服务端处理结果: {message.upper()}"
        time.sleep(5)
        await websocket.send(response)

async def main():
    async with websockets.serve(handle_connection, "localhost", 8765):
        print("WebSocket 服务已启动，监听 localhost:8765")
        await asyncio.Future()  # 保持服务运行

asyncio.run(main())